using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Deepcode.Flux.Core.Systems.CMS.Mappings;
using Deepcode.Flux.Core.Services.State;
using Deepcode.Flux.Core.Systems.Audit.Quick;

public partial class Private_CMS_Mapping_Default : Deepcode.Flux.Core.UI.AdministrationModulePage
{
	#region Permission Code Override
	public override string RequiredPermissionCode { get { return "FluxCMSMapAccess"; } }
	#endregion
	#region Page Load event
	/// <summary>
	/// Page Load event
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			LoadState();
			BindDataGrid();
		}
	}
	#endregion
	#region Initialisation
	/// <summary>
	/// OnInit - hooks up the data paging handler, 
	/// rpp change handler and item command handlers
	/// </summary>
	/// <param name="e"></param>
	protected override void OnInit(EventArgs e)
	{
		base.OnInit(e);
		datapager.OnRepage += new Deepcode.Flux.Core.UI.Controls.DataPager.PagerRepage(BindDataGrid);
		datapager.OnRPPChanged += new Deepcode.Flux.Core.UI.Controls.DataPager.RPPChanged(RowsPerPageChange);
		datagrid.ItemCommand += new DataGridCommandEventHandler(GridCommand);
	}
	#endregion
	#region Data binding and paging events
	/// <summary>
	/// Data binding
	/// </summary>
	private void BindDataGrid()
	{
		// Add javascript call to delete buttons
		datagrid.BindJavascript("btnDelete", "onclick", "return ConfirmDelete();");
		// Set rows per page from registry
		datapager.RowsPerPage = this.UserSetting("RowsPerPage", 10);

		// Get and bind the data
		DomainMappings mappings = new DomainMappings();
		datapager.DataSource = mappings.GetAll();
		datapager.DataBind();
	}
	/// <summary>
	/// Paging control has set a new number of rows per page - store in user registry
	/// </summary>
	/// <param name="NewRPP"></param>
	void RowsPerPageChange(int NewRPP)
	{
		this.SetUserSetting("RowsPerPage", NewRPP);
	}
	#endregion
	#region User interactions
	/// <summary>
	/// Command received from the data grid
	/// </summary>
	/// <param name="source"></param>
	/// <param name="e"></param>
	void GridCommand(object source, DataGridCommandEventArgs e)
	{
		if( e.Item.ItemType == ListItemType.Footer ||
			e.Item.ItemType == ListItemType.Header ||
			e.Item.ItemType == ListItemType.Pager ||
			e.Item.ItemType == ListItemType.Separator ) return;

		// Get the key of the row being actioned 
		int rowKey = (int) datagrid.DataKeys[e.Item.ItemIndex];

		// PROCESS COMMANDS
		if( e.CommandName == "Delete" )
		{
			DomainMappings mappings = new DomainMappings();
			DomainMapping map = mappings.GetByID( rowKey );
			mappings.DeleteByID( rowKey );

			QuickAudit.RaiseImportant(
				new AuditUser(),
				"Flux", "Mapping manager",
				"deleted mapping {0} ({1})",
				map.PK_ID, map.Domain);

			this.BindDataGrid();
			return;
		}
		if( e.CommandName == "Edit" )
		{
			string url = String.Format("ui_form.aspx?ID={0}&state={1}", rowKey, this.SaveState());
			Response.Redirect( url, true );
			return;
		}
	}
	/// <summary>
	/// User clicked the add new button
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnAddNew_Click(object sender, EventArgs e)
	{
		string url = String.Format("ui_form.aspx?state={0}", this.SaveState());
		Response.Redirect(url, true);
	}
	#endregion
	#region State Remember/Recall
	/// <summary>
	/// Load current state from a string on the url
	/// </summary>
	private void LoadState()
	{
		StateRemember r = new StateRemember(Request["state"]);
		datapager.StartRow = r.Recall("StartRow", 1);
	}
	/// <summary>
	/// Save current state to a string for later recall
	/// </summary>
	/// <returns></returns>
	public string SaveState()
	{
		StateRemember r = new StateRemember();
		r.Remember("StartRow", datapager.StartRow);
		return r.ToString();
	}
	#endregion
}
